<?php


namespace app\admin\controller;


use Monolog\Handler\IFTTTHandler;
use think\Db;

class Fengqiao
{
    public function receive()
    {
            $res =[
                'return_code' => '1000',
                'return_msg'   => '系统异常',
            ];

           $arrays = file_get_contents("php://input",'r');
           $array = json_decode($arrays,true);

           if (empty($array['Body']['WaybillRoute']))
           {
               echo '不存在';
               $res = json_encode($res);
               return $res;
           }
        $WaybillRoute = $array['Body']['WaybillRoute'];

        Db::startTrans();
        $order = '';
        try {

            foreach ($WaybillRoute as $key => $value)
            {
                 $order = $value['mailno'];
                 $courierData = Db::name('courier')
                    ->where('order_pay_sn',$value['orderid'])
                    ->where('waybill',$value['mailno'])
                    ->find();

                 if ($courierData)
                 {
                     $acceptTime = strtotime($value['acceptTime']);
                     $upData = [
                                 'updatetime' => $acceptTime,
                                 'opcodeRemak' => $value['remark'],
                                 'opcode' => $value['opCode'],

                     ];

                     if ($value['opCode'] == '50')
                     {
                         $upData['filterStatus'] = 2;
                         $upData['filtertime']   = $acceptTime;
                         $upData['pushresult']   = $arrays;
                     }

                     $courierRes = Db::name('courier')
                         ->where('order_pay_sn',$value['orderid'])
                         ->where('waybill',$value['mailno'])
                         ->update($upData);
                 }
            }
            file_put_contents('courier','执行时间: '.' 错误信息:'.'22222222'.' 快递单号:');
            if (!empty($courierRes))
            {
                $res = [
                    'return_code' => '0000',
                    'return_msg'   => '成功',
                ];
                Db::commit();
            }
        }catch(\Exception $e){
            $path = 'log';
            if (!file_exists($path))
            {
                mkdir($path,0777);
            }
            $time = date('Y-m-d H:i:s');

            file_put_contents($path.'/courier','执行时间: '.$time.' 错误信息:'.$e->getMessage().' 快递单号:'.$order.PHP_EOL,FILE_APPEND);
            Db::rollback();
        }
        $res = json_encode($res);
        return $res;
    }
}